#include<bits/stdc++.h>
#define int long long
using namespace std;
int siz[400005],fa[400005];
vector<int>s[400005];
struct f{int dfn,low,siz,from;}tr[400005];
int cnt,st[400005],top;
void dfs(int p,int fa){
	top++;st[top]=p;cnt++;
	int pd=0;tr[p].dfn=tr[p].low=cnt;
	for(int i=0;i<s[p].size();i++){
		int y=s[p][i];if(y==fa&&pd==0){pd=1;continue;}
		if(!tr[y].dfn){dfs(y,p);tr[p].low=min(tr[p].low,tr[y].low);}
		else{if(!tr[y].from)tr[p].low=min(tr[p].low,tr[y].dfn);}
	}
	if(tr[p].low==tr[p].dfn){
		int y;do{
			y=st[top];top--;
			tr[y].from=p;tr[p].siz++;
		}while(y!=p);
	}
}
int a[400005],b[400005],ans=0,pd=0,id[400005];
void dfs1(int p,int fa){ans+=tr[p].siz;
	if(tr[p].siz<=siz[p]) pd=1;id[p]=1;
	for(int i=0;i<s[p].size();i++){
		int y=s[p][i];if(y==fa) continue;
		dfs1(y,p);
	}
}
signed main(){
	freopen("pairint.in","r",stdin);
	freopen("pairint.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int root=0,n;cin>>n;
	for(int i=1;i<=2*n;i++){fa[i]=i,siz[i]=0;}
	for(int i=1;i<=n;i++){
		int x,y;cin>>x>>y;a[i]=x,b[i]=y;
		s[x].push_back(y);s[y].push_back(x);
	}
	for(int i=1;i<=2*n;i++){if(!tr[i].dfn)dfs(i,0);}
	for(int i=1;i<=2*n;i++) s[i].clear();
	for(int i=1;i<=n;i++){
		int x=a[i],y=b[i];
		if(tr[x].from==tr[y].from) siz[tr[x].from]++;
		else{
			s[tr[x].from].push_back(tr[y].from);
			s[tr[y].from].push_back(tr[x].from);
		}
	}
	for(int i=1;i<=2*n;i++){
		if(tr[i].from==i&&id[i]==0){pd=0;
			dfs1(i,0);ans=ans-(1-pd);
		}
	}
	cout<<ans;
	return 0;
}
